const tokenModule = {
  token() {
    return new Promise((resolve, reject) => {
      const refresh_token = vk.getStorageSync('refresh_token')
      const uni_id_token_expired = vk.getStorageSync('uni_id_token_expired')
      const currentTimestamp = Math.floor(Date.now() / 1000)
      console.log('🚀 ~ currentTimestamp:', currentTimestamp)
      console.log('uni_id_token_expired==> ', uni_id_token_expired)
      if (!refresh_token) {
        console.log('666==> ', 666)
        reject()
        return
      }
      if (refresh_token && currentTimestamp > uni_id_token_expired) {
        api
          .refreshToken({
            refreshToken: refresh_token
          })
          .then(result => {
            console.log('result==> ', result)
            if (result.code) {
              // 更新存储的令牌和过期时间
              vk.setStorageSync('uni_id_token', result.data.token)
              vk.setStorageSync('uni_id_token_expired', result.data.expire_time)

              resolve()
            } else {
              console.log('刷新失败,重新登录==> ')
              reject()
            }
          })
      } else {
        // 无刷新令牌,直接导航
        console.log('无刷新令牌,直接导航==> ')
        resolve()
      }
    })
  }
}

// 导出登录模块
export default tokenModule
